--
-- Author: shunguo.chen
-- Date: 2018-05-02 11:35:50
--
-- IMModel_share IMModel内和分享相关的数据

local ClassRef = import(".IMModel")

---------------------------------- share data ----------------------------------
-- 此处参赛需根据不同类型解析
function ClassRef:getShareData(tipsType, param1, param2, param3)
	assert(param1 ~= nil, "IMModel.getShareData param1 can't be nil")
	-- printf("IMModel.getShareData tipsType=%s, id=%s", tipsType, tid)
	if tipsType == Enums.TipsType.equip then
        -- tid, uid, puid
		return self:getEquipShareData(param1, param2, param3)
	elseif tipsType == Enums.TipsType.mount then
        -- tid, uid
		return self:getMountShareData(param1, param2)
	elseif tipsType == Enums.TipsType.task then
        -- tid
		return self:getTaskShareData(param1)
	elseif tipsType == Enums.TipsType.item then
        -- tid, uid
		return self:getItemShareData(tipsType, param1, param2)
	elseif tipsType == Enums.TipsType.achieve then
        -- tid, uid
		return self:getAchieveShareData(param1, param2)
	elseif tipsType == Enums.TipsType.jewel then
        -- tid, uid
		return self:getJewelShareData(param1, param2)
	elseif tipsType == Enums.TipsType.partner then
        -- tid, uid
		return self:getPartnerShareData(param1, param2)
	elseif tipsType == Enums.TipsType.fashion then
        -- tid, uid
		return self:getClothesShareData(param1, param2)
    elseif tipsType == Enums.TipsType.captainRoom then
        -- captainName, uid
        return  self:getCaptainRoomShareData(param1, param2)
	elseif tipsType == Enums.TipsType.duel then
        -- winName,failName,uid
		return  self:getDuelShareData(param1, param2, param3)
	else
		printf("IMModel.getShareData not serport tipsType=%s, tid=%s", tipsType, param1)
	end
end

function ClassRef:_getEquipShareData(tid, uid, puid)
	local conf = GD:getEquipConf(tid)
	if not conf then return end

	local nameR = conf.string or tid
	local name = L(nameR)
	local quality = tonumber(conf.quality or 1)

	local ret = {}
	ret.textShow = "[".. name .."]"
	local textAry = {"[e", name, tid, quality, uid}
	if puid then
		textAry[#textAry + 1] = puid
	end
    ret.textSend = table.concat(textAry, "|") .. "]"
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:_getMountShareData(tid, uid)
	local conf = GD:getMountConfig(tid)
	if not conf then return end

	local nameR = conf.name or tid
	local name = L(nameR)
	local ret = {}
    ret.textShow = "[".. name .."]"

	local data = UD:getMountByUid(uid)
    local ary = {"[m", name, tid}
    if data then
    	ary[#ary + 1] = data.quality
    	ary[#ary + 1] = data.starID .. "]"
    else
    	local quality = tonumber(conf.initRank or 1)
    	ary[#ary + 1] = quality .. "]"
    end
    ret.textSend = table.concat(ary, "|")
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:_getItemShareData(tipsType, tid, uid)
	local conf = GD:queryItemById(tid)
	local pre = "[i"

	if not conf then return end

	local nameR = conf.name or tid
	local name = L(nameR)
	local quality = tonumber(conf.quality or 1)
	local ret = {}
	ret.textShow = "[".. name .."]"
	local ary = {pre, name, tid, quality, uid.."]"}
    ret.textSend = table.concat(ary, "|")
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:_getJewelShareData(tid, uid)
	local conf = GD:queryJewelById(tid)
	if not conf then return end

	local nameR = conf.name or tid
	local name = L(nameR)
	local quality = tonumber(conf.quality or 1)
	local ret = {}
	ret.textShow = "[".. name .."]"
	local ary = {"[j", name, tid, quality, uid .. "]"}
    ret.textSend = table.concat(ary, "|")
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:_getPartnerShareData(tid, uid)
	local data = UD.partnersData:getPartnerById(tid)
	if not data then
		return
	end
	local conf = data:getConf()
	local nameR = conf.name or tid
	local name = data:getName()
	local quality = data:getQuality()
	local ret = {}
	ret.textShow = "[".. name .."]"
	local ary = {"[h", name, tid, quality, uid .. "]"}
    ret.textSend = table.concat(ary, "|")
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:_getClothesShareData(tid, uid)
	local conf = GD:getClothesConfig(tid)
	if not conf then return end

	local nameR = conf.name or tid
	local name = L(nameR)
	local quality = tonumber(conf.quality or 1)
	local ret = {}
	ret.textShow = "[".. name .."]"
	local ary = {"[z", name, tid, quality, uid .. "]"}
    ret.textSend = table.concat(ary, "|")
    ret.replace = {name, nameR}
    return ret
end

function ClassRef:getEquipShareData(tid, uid, puid)
	local ret
	local data = UD.equips[uid] or UD:getItems()[uid]
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			ret = self:_getEquipShareData(tid, uid, puid)
			data:setShareData(ret)
		end
	end
    return ret or self:_getEquipShareData(tid, uid, puid)
end
function ClassRef:getMountShareData(tid, uid)
	local ret
	local data = UD:getMountByUid(tostring(tid))
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			ret = self:_getMountShareData(tid, uid)
			data:setShareData(ret)
		end
	end
	return ret or self:_getMountShareData(tid, uid)
end
function ClassRef:getItemShareData(tipsType, tid, uid)
	local ret
	local data = UD:getItems()[uid]
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			ret = self:_getItemShareData(tipsType, tid, uid)
			data:setShareData(ret)
		end
	end
	return ret or self:_getItemShareData(tipsType, tid, uid)
end
function ClassRef:getTaskShareData(tid)
	local ret
	local data = UD:getTaskById(tid)
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			data:setShareData(ret)
		end
	end
	return ret
end
function ClassRef:getJewelShareData(tid, uid)
	local ret
	local data = UD:getJewel(uid)
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			ret = self:_getJewelShareData(tid, uid)
			data:setShareData(ret)
		end
	end
	return ret or self:_getJewelShareData(tid, uid)
end

function ClassRef:getPartnerShareData(tid, uid)
	local ret
	local data = UD.partnersData:getPartnerById(tid)
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else
			ret = self:_getPartnerShareData(tid, uid)
			data:setShareData(ret)
		end
	end
	return ret or self:_getPartnerShareData(tid, uid)
end

function ClassRef:getClothesShareData(tid, uid)
	local ret
	local data = UD:getClothesById(tid)
	if data then
		-- 能通过uid找到，说明是自己的，获取缓存的分享数据
		local shareData = data:getShareData()
		if shareData then
			ret = shareData
		else

			ret = self:_getClothesShareData(tid, uid)
			data:setShareData(ret)
		end
	end
	return ret or self:_getClothesShareData(tid, uid)
end

function ClassRef:getAchieveShareData(tid, uid)
	local conf = GD:getAchievementConfig(tid)
	local nameR = conf.name or tid
	local name = L(nameR)
	local ret = {}
	ret.textShow = "[".. name .."]"
    ret.textSend = "[a|" .. name .. "|" .. tid .. "|"..tostring(uid).."]"
	ret.replace = {name, nameR}
    return ret
end

function ClassRef:getCaptainRoomShareData(captainName, uid)
	local nameR = "captainroom_share"
	local name = string.format(L("captainroom_share"), captainName)
	local ret = {}
	ret.textShow = "[".. name .."]"
    ret.textSend = "[o|" .. name .. "," .. captainName .. "|" .. uid .. "]"
	ret.replace = {name, nameR}
    return ret
end

function ClassRef:getDuelShareData(winName,failName,uid)
    local nameR = "duelGametips38"
    local name = string.format(L(nameR), winName, failName)
    local ret = {}
    ret.textShow = "["..name.."]"
    ret.textSend = "[d|" .. name .. "," .. winName .. "," .. failName .."|" .. uid.. "]"
    ret.replace = {name, nameR}
    return ret
end

-- Emoji = 1
-- Equip = 2
-- Mount = 3
-- Task = 4 -- 弃用
-- Jewel = 4
-- Partner = 5
function ClassRef:getChatBoxData(boxType)
	if boxType == Enums.ImmPanelType.ImmPanel_Smile then
		return self:getEmojiData()
	elseif boxType == Enums.ImmPanelType.ImmPanel_Equip then
		return self:getEquipData(true)
	elseif boxType == Enums.ImmPanelType.ImmPanel_Mount then
		return self:getMountData(true)
	elseif boxType == Enums.ImmPanelType.ImmPanel_Jewel then
		-- return self:getTaskData()
		return self:getJewelData(true)
	elseif boxType == Enums.ImmPanelType.ImmPanel_Item then
		return self:getItemData(true)
	elseif boxType == Enums.ImmPanelType.ImmPanel_Partner then
		return self:getPartnerData(true)
	elseif boxType == Enums.ImmPanelType.ImmPanel_Fashion then
		return self:getClothesData(true)
	end
end

function ClassRef:getEmojiData()
	if nil == self._emojiDatas then
		self._emojiDatas = GD:getEmojiConfs()
		for i,v in pairs(self._emojiDatas) do
			v.icon = "art/chat_icon/".. v.path ..".png" 
			v.text = "#" .. string.sub( v.path, 2, -1 ) --去掉表情中的s,跟坐骑组合在一起会被屏蔽
	    end
	end
    return self._emojiDatas
end

function ClassRef:getEquipData(isRefresh)
	isRefresh = isRefresh or false
	if nil == self._equipDatas or isRefresh then
	    local playerEquips = {}
        for _,v in pairs(UD.equips) do
			self:getEquipShareData(v.tid, v.uid, UD:getUid())
			playerEquips[#playerEquips + 1] = v
        end

        local itemEquips = {}
        for _,v in pairs(UD:getItems()) do
            if v.itemBagType==Enums.ItemBagType.equip then
                self:getEquipShareData(v.tid, v.uid, UD:getUid())
				itemEquips[#itemEquips + 1] = v
            end
        end

		-- 装备（品质>位置>等级>id）> 未装备（品质>位置>等级>id）
		local function sort(a, b)
			if a.quality == b.quality then
				if a.location == b.location then
					if a.useLv == b.useLv then
						return a.tid < b.tid
					else
						return a.useLv > b.useLv
					end
				else
					return a.location < b.location
				end
			else
				return a.quality > b.quality
			end
		end
		table.sort(playerEquips, sort)
		table.sort(itemEquips, sort)

		self._equipDatas = table.attach(playerEquips, itemEquips)
	end
	return self._equipDatas
end

function ClassRef:getMountData()
	local mountModel = app:getInst("MountModel")
	local mounts = mountModel:getHaveMounts()
	for _,v in pairs(mounts) do
			self:getMountShareData(v.tid, v.uid)
    end
	return mounts
	--[[
	if nil == self._mountDatas or isRefresh then
	    self._mountDatas = {}
	    local mounts, spRewardMounts = UD:getAllActivityMounts()

        for _,v in pairs(mounts) do
			self:getMountShareData(v.tid, v.uid)
            table.insert(self._mountDatas, v)
        end

        if spRewardMounts then
            for _,v in pairs(spRewardMounts) do
				self:getMountShareData(v.tid, v.uid)
	            table.insert(self._mountDatas, v)
        	end
        end

        table.sort(self._mountDatas, function(a, b)
        		if a.quality == b.quality then
        		    if a.star == b.star then
        		        return a.tid < b.tid
        		    else
        		        return a.star > b.star
        		    end
        		else
        		    return a.quality > b.quality
        		end
        	end)
	end
	return self._mountDatas
	]]
end

function ClassRef:getJewelData(isRefresh)
	isRefresh = isRefresh or false
	if nil == self._jewelDatas or isRefresh then
		self._jewelDatas = {}
        for _,v in pairs(UD:getAllJewels()) do
			self:getJewelShareData(v.tid, v.uid)
            table.insert(self._jewelDatas, v)
        end
        table.sort(self._jewelDatas, function(a, b)
			if a.orderLevel ~= b.orderLevel then
				return a.orderLevel < b.orderLevel
			elseif a.quality ~= b.quality then
				return a.quality > b.quality
			elseif a.lv ~= b.lv then
				return a.lv > b.lv
			else
				return a.tid >b.tid
			end
		end)
	end
    return self._jewelDatas
end

function ClassRef:getPartnerData(isRefresh)
	isRefresh = isRefresh or false
	if nil == self._partnerDatas or isRefresh then
	    self._partnerDatas = {}
	    local partners = UD.partnersData:getAlreadyHavePartners()
        for _,v in pairs(partners) do
			self:getPartnerShareData(v:getId(), v:getId())
            table.insert(self._partnerDatas, v)
        end
        -- if spRewardPartners then
        --     for _,v in pairs(spRewardPartners) do
		-- 		self:getPartnerShareData(v.id, v.id)
	    --         table.insert(self._partnerDatas, v)
        -- 	end
        -- end

		table.sort(self._partnerDatas, function(a, b)
				local sp_a = a:isSpReward()
				local sp_b = b:isSpReward()
				if sp_a == sp_b then
					local star_a = a:getStar()
					local star_b = b:getStar()
					if star_a ~= star_b then
						return star_a > star_b
					else
						local sort_a = a:getSort()
						local sort_b = b:getSort()
						if sort_a == sort_b then
							return a:getId() > b:getId()
						else
							return sort_a < sort_b
						end
					end
				else
					if sp_a then
						return true
					end
					return false
				end
		       
        	end)
	end
    return self._partnerDatas
end

function ClassRef:getClothesData(isRefresh)
	isRefresh = isRefresh or false
	if nil == self._clothesDatas or isRefresh then
	    self._clothesDatas = {}
	    local clothes, spRewardClothes = UD:getAllActivityClothes()
        for _,v in pairs(clothes) do
			self:getClothesShareData(v.tid, v.tid)
            table.insert(self._clothesDatas, v)
        end

        if spRewardClothes then
            for _,v in pairs(spRewardClothes) do
				self:getClothesShareData(v.tid, v.tid)
	            table.insert(self._clothesDatas, v)
        	end
        end

        table.sort(self._clothesDatas, function(a, b)
        		local a_seat = a:getQuality()
				local b_seat = b:getQuality()
				local aInfo = a.conf
				local bInfo = b.conf

				local aIsNewGet = UD:isNewGetClothes(a.tid)
				local bIsNewGet = UD:isNewGetClothes(b.tid)

				if aIsNewGet ~= bIsNewGet then
					return aIsNewGet
				end

				if a_seat ~= b_seat then
					return a_seat < b_seat
				end

				if aInfo.type2icon ~= bInfo.type2icon then
					return not bInfo.type2icon
				end

				if aInfo.orderIndex ~= bInfo.orderIndex then
					return tonumber(aInfo.orderIndex) < tonumber(bInfo.orderIndex)
				end

				return tonumber(aInfo.ID) < tonumber(bInfo.ID)
        	end)
	end
    return self._clothesDatas
end

function ClassRef:getTaskData()
	-- 废弃了，暂时不分享任务
end

function ClassRef:getItemData(isRefresh)
	isRefresh = isRefresh or false
	if nil == self._itemDatas or isRefresh then
	    local items = {}
        for _,v in pairs(UD:getItems()) do
        	if v.itemBagType == Enums.ItemBagType.item then
				self:getItemShareData(Enums.TipsType.item, v.tid, v.uid)
				items[#items + 1] = v
			end
        end

	    -- 品质>等级
	    local function sort(a, b)
	    	if a.orderIndex == b.orderIndex then
		    	if a.quality == b.quality then
		    		if a.useLv == b.useLv then
		    			return a.tid < b.tid
		    		else
		    			return a.useLv > b.useLv
		    		end
		    	else
		    		return a.quality > b.quality
		    	end
		    else
		    	return a.orderIndex < b.orderIndex
		    end
	    end
        table.sort(items, sort)

        self._itemDatas = items
	end
    return self._itemDatas
end

return ClassRef